package com.engine.jucailinkq.attendance.attendanceanalysis.web;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.AttendanceSummaryService;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.impl.AttendanceSummaryServiceImpl;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: sy
 * @Description: 出勤汇总数据相关功能接口
 * @Date: 2024/3/25
 **/
public class AttendanceSummaryApi {
    BaseBean bs = new BaseBean();
    private AttendanceSummaryService getAttendanceSummaryService(User user) {
        return ServiceUtil.getService(AttendanceSummaryServiceImpl.class,user);
    }

    /**
     * 出勤汇总报表查询条件
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/getCqSummarySearchCondition")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCqSummarySearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request, response);
        apidatas = getAttendanceSummaryService(user).getCqSummarySearchCondition(ParamUtil.request2Map(request));
        return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取出勤汇总报表
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/getCqSummaryReport")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCqSummaryReport(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request, response);
        apidatas = getAttendanceSummaryService(user).getCqSummaryReport(ParamUtil.request2Map(request));
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 新增出勤汇总数据
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/addAttendanceSummary")
    @Produces(MediaType.APPLICATION_JSON)
    public String addAttendanceSummary(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request, response);
        apidatas = getAttendanceSummaryService(user).addAttendanceSummary(ParamUtil.request2Map(request));
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 导出出勤汇总报表
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/exportExcel")
    @Produces(MediaType.TEXT_PLAIN)
    public String exportExcel(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getAttendanceSummaryService(user).exportCqSummaryReport(ParamUtil.request2Map(request), request, response);
        } catch (Exception e) {
            apidatas.put("status", "-1");
            bs.writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    @GET
    @Path("/refreshViewAttendanceDaily")
    @Produces(MediaType.APPLICATION_JSON)
    public String refreshViewAttendanceDaily(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request, response);
        apidatas = getAttendanceSummaryService(user).refreshViewAttendanceDaily();
        return JSONObject.toJSONString(apidatas);
    }
}
